package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.domain.Slideshow;
import com.briup.exception.ServiceException;
import com.briup.resuit.ResultCode;
import com.briup.service.SlideshowService;
import com.briup.mapper.SlideshowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author 白宇杰
 * @description 针对表【cms_slideshow】的数据库操作Service实现
 * @createDate 2024-11-18 14:40:04
 */
@Service
public class SlideshowServiceImpl extends ServiceImpl<SlideshowMapper, Slideshow>
        implements SlideshowService {
    @Autowired
    SlideshowMapper slideshowMapper;
    @Override
    public void addOrUpdate(Slideshow slideshow) {
        //boolean flag = true;
        Integer id = slideshow.getId();
        String url = slideshow.getUrl();
        LambdaQueryWrapper<Slideshow> lqw = new LambdaQueryWrapper<>();
        lqw.select(Slideshow::getUrl).eq(url != null, Slideshow::getUrl, url);
        List<Slideshow> urls = slideshowMapper.selectList(lqw);
        if (urls != null && !urls.isEmpty()) {
            throw new ServiceException(ResultCode.FAIL);
        }
        if (Objects.isNull(id)) {

            slideshow.setDeleted(0);
            slideshow.setUploadTime(new Date());
            slideshowMapper.insert(slideshow);
        } else {

            slideshowMapper.updateById(slideshow);
        }
    }
}




